package com.achen.controller;

import com.achen.constant.MessageConstant;
import com.achen.entity.Result;
import com.achen.pojo.OrderSetting;
import com.achen.service.OrderSettingService;
import com.achen.utils.POIUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 预约设置管理
 * @author achen
 * @create 2020-04-12 11:35
 */
@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;

    //文件上传，实现预约设置批量导入
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile){
        try{
            //使用poi解析表格
            List<String[]> list = POIUtils.readExcel(excelFile);
            //创建一个新的集合存储预约设置数据
            List<OrderSetting> orderSettingList = new ArrayList<>();
            for(String[] strings : list){
                String ordersettingDate = strings[0]; //excel表格第一列为预约设置
                String number = strings[1]; //excel表格第二列为预约设置人数
                //将预约日期和预约人数封装到OrderSetting对象中
                OrderSetting orderSetting = new OrderSetting(new Date(ordersettingDate),Integer.parseInt(number));
                //讲预约设置批量添加到集合中
                orderSettingList.add(orderSetting);
            }
            //通过dubbo远程调用读物实现数据批量导入到数据库
            orderSettingService.addOrderSetting(orderSettingList);
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }

    //根据月份查询相应的预约设置数据
    @RequestMapping("/findOrderSettingByMonth")
    public Result findOrderSettingByMonth(String date){
        try{
            //存map是因为要与前台展示数据的格式保持一致
            List<Map> list = orderSettingService.findOrderSettingByMonth(date);
            return new Result(true,MessageConstant.GET_ORDERSETTING_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }

    //根据日期查询相应的预约设置数据
    @RequestMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){
        try{
            //存map是因为要与前台展示数据的格式保持一致
            orderSettingService.editNumberByDate(orderSetting);
            return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }
    }
}
